package com.hand.liut.controller;

import ch.qos.logback.classic.db.names.ColumnName;
import com.hand.liut.entity.CommonResult;
import com.hand.liut.entity.Payment;
import javax.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: 刘涛
 * @Description: TODO
 * @DateTime: 2021/7/16 14:14
 **/
@RestController
public class PaymentController {

  public static final String PAYMENT_SRV = "http://cloud-provider-payment";
  @Resource
  private RestTemplate restTemplate;

  @GetMapping("/consumer/create")
  public CommonResult<Payment> create(Payment payment) {
    return restTemplate
        .postForObject(PAYMENT_SRV + "/consumer/create", payment, CommonResult.class);
  }

  @GetMapping("/consumer/find/{id}")
  public CommonResult<Payment> findById(@PathVariable("id") Long id) {
    return restTemplate.getForObject(PAYMENT_SRV + "/find/" + id, CommonResult.class, id);
  }

  @GetMapping("/consumer/find1/{id}")
  public CommonResult<Payment> findById1(@PathVariable("id") Long id) {
    ResponseEntity<CommonResult> responseEntity = restTemplate
        .getForEntity(PAYMENT_SRV + "/find/" + id, CommonResult.class, id);
    if (responseEntity.getStatusCode().is2xxSuccessful()) {
      return responseEntity.getBody();
    } else {
      return new CommonResult<Payment>(444, "操作失败");
    }
  }
}
